<template>
	<view>
		<MySearch @click="search"></MySearch>
		<!-- <MySearch bgcolor="pink" :radius="6"></MySearch> -->
		<view class="scroll-box">
			<!-- 左侧scroll -->
			<scroll-view scroll-y="true" class="scroll-left">
				<view v-for="(item, i) in cateList" :key="item.cat_id" :class="{ active: i===current }" @click="leftClick(i)">
					{{ item.cat_name }}
				</view>
			</scroll-view>
			<!-- 右侧scroll -->
			<scroll-view scroll-y="true" class="scroll-right" :scroll-top="scrollTop">
				<view class="right-item" v-for="(item, i) in cateList[current].children" :key="i">
					<view class="scroll-item-title">
						/ {{ item.cat_name }} /
					</view>
					<view class="scroll-item-body">
						<!-- 三级分类 -->
						<view class="" v-for="(item1, i1) in item.children" :key="i1" @click="goodsClick(item1)">
							<image :src="item1.cat_icon" mode="widthFix"></image>
							<text>{{ item1.cat_name }}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import MySearch from '../../components/my-search/my-search.vue'
	import { getCateList } from '../../api/index.js'
	import badge from '../../mixins/cart-badge.js'
	export default {
		mixins: [badge],
		data() {
			return {
				cateList: [],
				current: 0,
				scrollTop: 0
			};
		},
		onLoad() {
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const { message } = await getCateList()
				this.cateList = message
			},
			leftClick(i) {
				this.current = i
				// 每次切换重新返回顶部 但是赋值不能相同只能这样切换
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			goodsClick(item) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${item.cat_id}`
				})
			},
			search() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
		components: {
			MySearch
		}
	}
</script>

<style scoped lang="scss">
.scroll-box {
	display: flex;
	.scroll-left {
		width: 120px;
		height: 1010rpx;
		background-color: #f7f7f7;
		font-size: 24rpx;
		view {
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #ddd;
			&.active {
				position: relative;
				background-color: #fff;
				&::before {
					content: ' ';
					display: block;
					width: 6rpx;
					height: 50rpx;
					background-color: #f00;
					border: none;
					position: absolute;
					top: 50%;
					left: 5rpx;
					transform: translateY(-50%);
				}
			}
			
		}
		
	}
	.scroll-right {
		height: 1010rpx;
		padding: 0 20rpx;
		background-color: #fff;
		.right-item {
			.scroll-item-title {
				text-align: center;
				line-height: 70rpx;
				font-size: 32rpx;
				font-weight: 700;
				color: #444;
			}
			.scroll-item-body {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				text-align: center;
				view {
					width: 33.3%;
					padding: 15rpx 0;
					image {
						width: 85%;
					}
					text {
						display: block;
						font-size: 24rpx;
					}
					
				}
			}
		}
	}
}
</style>
